<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="../vue.js"></script>
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <input
        type="text"
        v-model="ipt"
        placeholder="请输入你要添加的课程"
        @keyup.enter="addCourse"
      />
      <br />
      <button @click="addCourse">添加</button>
      <div v-for="item in courses">{{item}}</div>
      <div>总课程数量--{{total}}</div>
      <h3>a的值:{{number.a.b.c.d}}</h3>
      <button @click="number.a.b.c.d++;">点击a++</button>
    </div>
    <script>
      var app = new Vue({
        el: "#app",
        data: {
          number: {
            a: {
              b: {
                c: {
                  d: 123,
                },
              },
            },
          },
          ipt: "",
          courses: ["html", "css"],
          total: 0,
        },
        watch: {
          //要监视谁
          courses: {
            immediate: true, //立即监视这样页面一打开 课程总数就会显示正常
            handler(newValue) {
              // console.log(newValue);
              this.total = newValue.length;
            },
          },
          // a(newValue) {
          //   console.log(newValue);
          // },
          // "number.a": {
          //   handler(newValue) {
          //     console.log(newValue);
          //   },
          // },
          number: {
            //深度检测，不管你的对象有几层
            deep: true,
            handler() {
              console.log("number改变了");
            },
          },
        },
        methods: {
          addCourse() {
            this.courses.push(this.ipt);
          },
        },
      });
    </script>
  </body>
</html>
